import network_utils from '../../../../../http/NetworkUtils.js';
import constants from '../../../../../common/Constants.js';
import * as umfToast from '../../../../../utils/UmfToast';
import *as regexUtils from '../../../../../utils/RegexUtils'
import * as UmfDialog from '../../../../../utils/UmfDialog';

import {$Toptips, $Toast} from '../../../../../components/CompRouter';

var OriginData = {};

var AgentId = "";

Page({

    /**
     * 页面的初始数据
     */
    data: {},

    onLoad: function (options) {
        AgentId = options.agentId;
        this.requestAgInfo()
    },

    requestAgInfo: function () {
        network_utils.buildRequest(constants.Interfaces.FUNC_SEARCH_AGENT_DETAILS, {agentId: AgentId}, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    this._requestSuccess(response);
                    return true
                }
                return false
            }
        }).send();
    },

    _requestSuccess(response) {
        let agentBasiInfo = [];
        if (response) {
            agentBasiInfo.push({
                itemTitle: "代理商全称", itemName: "agentName",
                content: response["agentName"], typeStr: "text", placeholderStr: "请输入代理商全称"
            });
            agentBasiInfo.push({
                itemTitle: "代理商简称", itemName: "agentShortName",
                content: response["agentShortName"], typeStr: "text", placeholderStr: "请输入代理商简称"
            });
            agentBasiInfo.push({
                itemTitle: "组织机构代码", itemName: "registrationNo",
                content: response["registrationNo"], typeStr: "text", placeholderStr: "请输入组织机构代码"
            });
            agentBasiInfo.push({
                itemTitle: "工商登记号", itemName: "busiRegistNum",
                content: response["busiRegistNum"], typeStr: "text", placeholderStr: "请输入工商登记号"
            });
            agentBasiInfo.push({
                itemTitle: "联系人", itemName: "userName",
                content: response["userName"], typeStr: "text", placeholderStr: "请输入联系人姓名"
            });
            agentBasiInfo.push({
                itemTitle: "联系人手机", itemName: "userMobile",
                content: response["userMobile"], typeStr: "number", placeholderStr: "请输入联系人手机号"
            });

            this.setData({
                agentBasiInfo: agentBasiInfo,
                assistId: response["assistId"],
                agentMail: response["email"]
            });

            //保存初始值
            OriginData = response;
            OriginData.agentMail = response["email"];
        }
    },

    /**
     * 提交修改
     * @param e
     */
    accInfoSubmit: function (e) {
        let params = e.detail.value;

        if (!this._isParamsChange(params)) {
            $Toast.showText("您并未修改任何信息");
            return;
        }

        if (this._checkForm(params)) {
            UmfDialog.show("是否提交修改代理商信息", () => {
                this._requestModified(params)

            }, true);
        }
    },

    tapResetPwd: function (e) {
        UmfDialog.show("是否重置代理商登录密码", () => {
            let params = {
                assistId: this.data.assistId,
                agentId: AgentId
            };

            network_utils.buildRequest(constants.Interfaces.FUNC_RESET_AGENTPWD, params, {
                doHttpSucess: (response) => {
                    if (constants.RetCodes.SUCCESS === response.retCode) {
                        $Toast.showText("业务员登录密码已重置为111111");
                    }
                    return false
                }
            }).send();
        }, true);


    },

    _requestModified: function (params) {

        params['agentId'] = AgentId;

        network_utils.buildRequest(constants.Interfaces.FUNC_MODIFY_AGENTINFO, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    umfToast.showToast('代理商信息修改成功');
                    setTimeout(() => {
                        wx.navigateBack({
                            delta: 1
                        })
                    }, 1000)
                }
                return false
            }
        }).send();
    },

    _checkForm: function (params) {
        let regexArr = [];
        // regexArr.push([params.agentName, constants.RegexTypes])//todo
        // regexArr.push([params.agentShortName, constants.RegexTypes])//todo
        regexArr.push([params.registrationNo, constants.RegexTypes.REGISTRATIONNO])
        // regexArr.push([params.busiRegistNum, constants.RegexTypes])//todo
        // regexArr.push([params.userName, constants.RegexTypes])//todo
        regexArr.push([params.userMobile, constants.RegexTypes.MOBILE])
        regexArr.push([params.agentMail, constants.RegexTypes.LINKTELEMAIL])

        let errorMsg = regexUtils.regexTypesWithMsg(regexArr);
        if (errorMsg) {
            $Toptips.error({text: errorMsg});
            return false;
        } else {
            return true;
        }
    },

    _isParamsChange(params) {
        for (let key of Object.keys(params)) {
            if (params[key] !== OriginData[key]) {
                return true;
            }
        }
        return false;
    }

})